using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame.MapComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CloudSpawnerComponent : Microsoft.Xna.Framework.GameComponent
    {
        Texture2D cloud1, cloud2, cloud3;

        CloudComponent clouds;
        List<CloudComponent> cloudsList;

        Camera2DComponent camera;

        Random rnd;

        public CloudSpawnerComponent(Game game, Camera2DComponent camera)
            : base(game)
        {
            this.camera = camera;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            //Create list for storing clouds
            cloudsList = new List<CloudComponent>();
            rnd = new Random();

            base.Initialize();
        }

        public void LoadContent(ContentManager Content)
        {
            //Load graphics
            cloud1 = Content.Load<Texture2D>("GFX\\Terrain\\cloud1");
            cloud2 = Content.Load<Texture2D>("GFX\\Terrain\\cloud2");
            cloud3 = Content.Load<Texture2D>("GFX\\Terrain\\cloud3");
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            //Spawn
            if (rnd.Next(0, 500) == 1)
            {
                clouds = new CloudComponent(this.Game, new Vector2(camera.getPosition().X + 1000, rnd.Next(250, 800)), cloud1, rnd.NextDouble());
                clouds.Initialize(); //Initialize to set starting values
                cloudsList.Add(clouds);
            }
            else if (rnd.Next(0, 250) == 2)
            {
                clouds = new CloudComponent(this.Game, new Vector2(camera.getPosition().X + 1000, rnd.Next(250, 800)), cloud2, rnd.NextDouble());
                clouds.Initialize(); //Initialize to set starting values
                cloudsList.Add(clouds);
            }
            else if (rnd.Next(0, 350) == 3)
            {
                clouds = new CloudComponent(this.Game, new Vector2(camera.getPosition().X + 1000, rnd.Next(250, 800)), cloud3, rnd.NextDouble());
                clouds.Initialize(); //Initialize to set starting values
                cloudsList.Add(clouds);
            }


            //Update clouds
            for (int i = 0; i < cloudsList.Count; i++)
                cloudsList[i].Update(gameTime, i, camera, this);

            base.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //Draw every cloud
            for (int i = 0; i < cloudsList.Count; i++)
                cloudsList[i].Draw(spriteBatch);
        }

        public void Despawn(int index)
        {
            cloudsList.RemoveAt(index);
        }
    }
}
